package com.caigou.modules.forum.adpter

import android.graphics.Color
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.ext.GlideDisplay
import com.caigou.comm.base.utils.ImageLoader
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.forum.R
import com.caigou.modules.forum.bean.PostItemBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lwkandroid.widget.ngv.DefaultNgvAdapter
import com.lwkandroid.widget.ngv.NgvChildImageView
import com.lwkandroid.widget.ngv.NineGridView

/**
 * @author #Suyghur.
 * Created on 2021/11/20
 */
class ForumItemAdapter(dataset: MutableList<PostItemBean>) :
    BaseQuickAdapter<PostItemBean, BaseViewHolder>(R.layout.item_forum_list, dataset), LoadMoreModule {

    init {
        addChildClickViewIds(R.id.item_forum_ll_like)
    }

    override fun convert(holder: BaseViewHolder, item: PostItemBean) {
        if (TextUtils.isEmpty(item.headImg)) {
            holder.setImageResource(R.id.item_forum_iv_avatar, R.drawable.img_comm_avatar)
        } else {
            ImageLoader.loadCircle(context, item.headImg, holder.getView(R.id.item_forum_iv_avatar))
        }

        if (item.essence == 1) {
            holder.getView<ImageView>(R.id.item_forum_tv_start).visibility = View.VISIBLE
            ImageLoader.load(context, item.essenceIcon, holder.getView(R.id.item_forum_tv_start))
        } else {
            holder.getView<ImageView>(R.id.item_forum_tv_start).visibility = View.GONE
        }

        if (item.isDare == 1) {
            holder.getView<TextView>(R.id.item_forum_tv_dare).visibility = View.VISIBLE
        } else {
            holder.getView<TextView>(R.id.item_forum_tv_dare).visibility = View.GONE
        }

        holder.setText(R.id.item_forum_tv_nickname, item.nickName)
        if (item.vip == 1) {
            holder.getView<TextView>(R.id.item_forum_tv_nickname).setTextColor(Color.parseColor("#FE6880"))
        } else {
            holder.getView<TextView>(R.id.item_forum_tv_nickname).setTextColor(Color.parseColor("#505050"))
        }

        holder.setText(R.id.item_forum_tv_time, item.createTime)

        if (item.type == 5) {
            holder.setText(R.id.item_forum_tv_period, item.fifaTypeName)
            holder.getView<TextView>(R.id.item_forum_tv_period).setBackgroundColor(Color.parseColor("#6C99E9"))

        } else {
            holder.setText(R.id.item_forum_tv_period, item.period.toString())
            if (item.currPeriod) {
                holder.getView<TextView>(R.id.item_forum_tv_period).setBackgroundColor(Color.parseColor("#6C99E9"))
            } else {
                holder.getView<TextView>(R.id.item_forum_tv_period).setBackgroundColor(Color.parseColor("#B9B9B9"))
            }
        }

        holder.getView<ImageView>(R.id.item_forum_iv_redpack).visibility = if (item.redPacketId == 0) {
            View.GONE
        } else {
            View.VISIBLE
        }

        holder.setText(R.id.item_forum_tv_title, item.title)
        holder.setText(R.id.item_forum_tv_content, item.content)

        holder.getView<NineGridView>(R.id.item_forum_ngv_display).run {
            if (item.imgs.size > 0) {
                val imgs = if (item.imgs.size > 3) {
                    item.imgs.slice(1..3)
                } else {
                    item.imgs
                }
                visibility = View.VISIBLE
                setSingleImageSize(TypedValue.COMPLEX_UNIT_DIP, 150, 200)
                val ngvAdapter = DefaultNgvAdapter(100, GlideDisplay())
                ngvAdapter.setOnChildClickListener(object : DefaultNgvAdapter.OnChildClickedListener<String> {
                    override fun onPlusImageClicked(plusImageView: ImageView?, dValueToLimited: Int) {

                    }

                    override fun onContentImageClicked(position: Int, data: String?, childImageView: NgvChildImageView?) {
                        ARouter.getInstance().build(RouterPath.Forum.PAGE_FORUM_POST)
                            .withInt(AppConstants.POST_ID, item.id)
                            .navigation()
                    }

                    override fun onImageDeleted(position: Int, data: String?) {
                    }

                })
                ngvAdapter.dataList = imgs
                adapter = ngvAdapter
            } else {
                visibility = View.GONE
            }
        }

        holder.setText(R.id.item_forum_tv_like_num, item.nicePointCount.toString())
        if (item.like) {
            holder.setImageResource(R.id.item_forum_iv_like, R.drawable.img_forum_like)
            holder.getView<TextView>(R.id.item_forum_tv_like_num).setTextColor(Color.parseColor("#FE6880"))
        } else {
            holder.setImageResource(R.id.item_forum_iv_like, R.drawable.img_forum_unlike)
            holder.getView<TextView>(R.id.item_forum_tv_like_num).setTextColor(Color.parseColor("#505050"))
        }

        holder.setText(R.id.item_forum_tv_browse_num, item.browseCount.toString())
        holder.setText(R.id.item_forum_tv_comment_num, item.replyCount.toString())
    }
}